package com.have.blog.web.service.impl;

import com.have.blog.base.constant.BaseMessageConstant;
import com.have.blog.base.constant.ErrorCode;
import com.have.blog.base.exception.QueryException;
import com.have.blog.model.mapper.SysParamsMapper;
import com.have.blog.model.models.SysParams;
import com.have.blog.model.models.SysParamsExample;
import com.have.blog.web.service.SystemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@SuppressWarnings("all")
public class SystemParamServiceImpl implements SystemParamService {
//    @Autowired
//    private AdminSystemParamCacheService systemParamCacheService;
    @Autowired
    private SysParamsMapper sysParamsMapper;

    /**
     * 通过关键词获取系统参数的值
     * @param paramKey
     * @return
     */
    @Override
    public String getSystemParamValueByKey(String paramKey) {

//        SysParams sysParams = systemParamCacheService.getSystemParamByParamKey(paramKey);
//        if (sysParams != null) {
//            return sysParams.getParamsValue();
//        }
        SysParamsExample example = new SysParamsExample();
        example.createCriteria()
                .andParamsKeyEqualTo(paramKey);
        List<SysParams> tSysParams = sysParamsMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(tSysParams)) {
            throw new QueryException(ErrorCode.PLEASE_CONFIGURE_SYSTEM_PARAMS, BaseMessageConstant.PLEASE_CONFIGURE_SYSTEM_PARAMS);
        }
        SysParams sysParams = tSysParams.get(0);
//        systemParamCacheService.saveSystemParamByParamKey(sysParams, paramKey, 1, TimeUnit.DAYS);
        return sysParams.getParamsValue();
    }
}
